package de.pass.ch.datastr;

public class BinaryHeapElement<T extends PQueueNode> {

	/** The external key of this element. */
	private int _key;

	/** The application-specific data that is associated with this element. */
	private T _data;

	/**
	 * This element's index within the heap. 0 is used to indicate that this
	 * element has been deleted from the heap.
	 */
	private int _index;

	/** Constructs a DUMMY element. */
	public BinaryHeapElement(T data) {
		this(Integer.MAX_VALUE, 0, data);
	}

	/**
	 * Constructor.
	 * 
	 * @param key
	 *            key of the new element
	 * @param index
	 *            index of the new element within the heap
	 */
	public BinaryHeapElement(int key, int index, T data) {
		_key = key;
		_index = index;
		_data = data;
	}

	/** Returns the external key of this element. */
	public int key() {
		return _key;
	}

	/** Sets the external key of this element. */
	public void key(int newKey) {
		_key = newKey;
	}

	public boolean isDummy() {
		return key() == Integer.MAX_VALUE;
	}

	/**
	 * Returns a reference to the application-specific data object of this
	 * element.
	 */
	public T data() {
		return _data;
	}

	/** Marks that this element has been deleted from the heap. */
	public void markDeleted() {
		index(0);
	}

	/** Returns true iff this element has been deleted from the heap. */
	public boolean hasBeenDeleted() {
		return (_index == 0);
	}

	/** Returns this element's index within the heap. */
	public int index() {
		return _index;
	}

	/** Sets this element's index within the heap. */
	public void index(int newIndex) {
		_index = newIndex;
	}

}
